from django.utils.decorators import method_decorator
from .models import UserInfo, System_LoginPage
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse, HttpResponseRedirect
from django.views import View
import requests
from myapp.roles import rolesoperate
class SessionJsonData(View):
    session = requests.Session()  # 创建会话对象

    @csrf_exempt
    def get(self, request, *args, **kwargs):
        # 获取保存在会话对象中的 response.json()
        saved_json = self.session.saved_json
        # print(saved_json)
        if not saved_json:
            return JsonResponse({'message': 'No saved JSON data found.'}, status=400)
        # 将 account 属性值存储为令牌
        username = saved_json['account']
        try:
            user_info = UserInfo.objects.get(username=username)
            permission = user_info.permission
            menus = rolesoperate.getmenus1(self,permission=permission)
            menus = rolesoperate.remove_empty_children(self,menus)
            menus = rolesoperate.get_ids_without_children(self,menus)
            login = user_info.login
        except UserInfo.DoesNotExist:
            menus = rolesoperate.getmenus1(self,'用户')
            UserInfo.objects.update_or_create(username=username,permission='用户',login=1,secret_key = "",phonenumber="",cardid=username,email=username+'@cyou-inc.com')
            return JsonResponse({'account': username, 'permission': '用户', 'login': 1, 'cardid': username,'menus':menus})
        return JsonResponse({'account': username,'permission':permission,'login':login,'cardid': username,'menus':menus})

    @csrf_exempt
    def post(self, request, *args, **kwargs):
        # 获取网页内容
        oauthtoken = request.GET.get('oauthtoken')
        BODY = f'oauthtoken={oauthtoken}'
        url = 'http://oauth.cyou-inc.com:8090/oauth/user/getAccount'
        response = requests.post(url=url, data=BODY)
        json_data = response.json()
        self.session.saved_json = json_data
        return HttpResponseRedirect('http://10.12.28.129:8088/aouth_login')

    @csrf_exempt
    def csrf(self, request, *args, **kwargs):
        url = "http://oauth.cyou-inc.com:8088/oauth/oauth.html?redirectUrl=http://10.12.28.129:8000/save_token"
        response = requests.get(url)
        if response.status_code == 200:
            return JsonResponse({'status': 200})
        else:
            return JsonResponse({'status': 400})

    #解决前端跨域请求问题接口
    @csrf_exempt
    def choose_loginpage(self, request, *args, **kwargs):
        system = request.GET.get('system')
        my_objects = System_LoginPage.objects.get(system=system)
        Login_Page = my_objects.Login_Page
        return JsonResponse({'Login_Page': Login_Page})

    @method_decorator(csrf_exempt)
    def dispatch(self, request, *args, **kwargs):
        action = kwargs.pop('action', None)
        if action == 'get_saved_json':
            return self.get(request, *args, **kwargs)
        elif action == 'save_token':
            return self.post(request, *args, **kwargs)
        elif action == 'csrf':
            return self.csrf(request, *args, **kwargs)
        elif action == 'choose_loginpage':
            return self.choose_loginpage(request, *args, **kwargs)
        else:
            return super().dispatch(request, *args, **kwargs)